package 集合.哈希;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class 七种遍历方式 {
    // 创建并赋值 HashMap
    Map<Integer, String> map = new HashMap();

    private void test(){//初始化
        map.put(1, "Java");
        map.put(2, "JDK");
        map.put(3, "Spring Framework");
        map.put(4, "MyBatis framework");
        map.put(5, "Java中文社群");
    }

    /**
     * 1、迭代器——EntrySet
     */
    private void IteratorEntrySet(){
        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, String> entry = iterator.next();
            System.out.print(entry.getKey());
            System.out.print(entry.getValue());
        }
    }

    private void IteratorEntry(){
        Iterator<Map.Entry<Integer,String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, String> entry = iterator.next();
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
        Iterator<Integer> iterator1 = map.keySet().iterator();
        for (Map.Entry<Integer,String> entry: map.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
        for (Integer key: map.keySet()) {
            System.out.println();
        }
        Iterator<Map.Entry<Integer,String>>iterator2 = map.entrySet().iterator();

        map.forEach((key,value) ->{
            System.out.println(key);
            System.out.println(value);
        });
        map.entrySet().stream().forEach((entry) -> {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });
        map.entrySet().parallelStream().forEach((entry) ->{
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });
    }

    /**
     * 2、迭代器——KeySet
     */
    private void IteratorKeySet(){
        Iterator<Integer> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            Integer key = iterator.next();
            System.out.print(key);
            System.out.print(map.get(key));
        }
    }
    /**
     * 3、ForEach——EntrySet
     */
    private void ForEachEntrySet(){
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            System.out.print(entry.getKey());
            System.out.print(entry.getValue());
        }
    }
    /**
     * 4、ForEach——KeySet
     */
    private void ForEachKeySet(){
        for (Integer key : map.keySet()) {
            System.out.print(key);
            System.out.print(map.get(key));
        }
    }
    /**
     * 5、Lambda
     */
    private void Lambda(){
        map.forEach((key, value) -> {
            System.out.print(key);
            System.out.print(value);
        });
    }
    /**
     * 6、Streams API单线程
     */
    private void StreamsAPISingleton(){
        map.entrySet().stream().forEach((entry) -> {
            System.out.print(entry.getKey());
            System.out.print(entry.getValue());
        });
    }
    /**
     * 7、Streams API多线程
     */
    private void StreamsAPIPrototype(){
        map.entrySet().parallelStream().forEach((entry) -> {
            System.out.print(entry.getKey());
            System.out.print(entry.getValue());
        });
    }


}
